﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web.Mvc;
using InfoTech.Core.Service;
using InfoTech.LanTranh.MucVu.Bussiness;

namespace InfoTech.LanTranh.MucVu.Service
{
    public class GIAOPHANService : BaseService
    {
        public static bool Check_Insert_GIAOPHAN(GIAOPHAN giaophan)
        {
            bool result = true;
            DataContext dataContext = CreateDataContext();
            result &= dataContext.GetTable<GIAOPHAN>().FirstOrDefault(c => c.TENGIAOPHAN.ToUpper() == giaophan.TENGIAOPHAN.ToUpper()) == null;
            return result;
        }

        public static bool Check_Update_GIAOPHAN(GIAOPHAN giaophan)
        {
            bool result = true;
            DataContext dataContext = CreateDataContext();
            result &= dataContext.GetTable<GIAOPHAN>().FirstOrDefault(c => c.TENGIAOPHAN.ToUpper() == giaophan.TENGIAOPHAN.ToUpper() &&
                c.MAGIAOPHAN != giaophan.MAGIAOPHAN) == null;
            return result;
        }

        public static bool Check_Delete_GIAOPHAN(string[] strings)
        {
            bool result = true;
            DataContext dataContext = CreateDataContext();
            result &= dataContext.GetTable<GIAOPHAN>().FirstOrDefault(c => strings.Contains(c.MAGIAOPHAN + "")) != null;
            return result;
        }

        public static IEnumerable<SelectListItem> Get_List()
        {
            DataContext dataContext = CreateDataContext();
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "--- {Chọn Giáo Phận} ---", Value = "" });
            list.AddRange(new SelectList(dataContext.GetTable<GIAOPHAN>().OrderBy(c => c.TENGIAOPHAN), "MAGIAOPHAN", "TENGIAOPHAN"));
            return list;
        }

        public static IEnumerable<SelectListItem> Get_List_Data()
        {
            DataContext dataContext = CreateDataContext();
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "--- {Chọn Giáo Phận} ---", Value = "" });
            list.AddRange(dataContext.GetTable<GIAOPHAN>().Where(c => c.ISDATA.HasValue ? c.ISDATA.Value : false).OrderBy(c => c.TENGIAOPHAN).Select(c => new SelectListItem() { Text = c.TENGIAOPHAN, Value = c.MAGIAOPHAN + "", Selected = c.ISDEFAULT ?? false }));
            return list;
        }

    }
}
